package com.ant.init;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

/**
 * 启动成功后输出
 *
 * @author liuchengbiao
 * @date 2020-06-23 10:52
 */
@Component
@Slf4j
@Order(1)
public class SuccessCommandLineRunner implements CommandLineRunner {

    @Autowired
    ResourceLoader resourceLoader;

    @Override
    public void run(String... args) throws Exception {
        // 读取logo文件
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            Resource resource = resourceLoader.getResource("classpath:success.txt");
            is = resource.getInputStream();
            isr = new InputStreamReader(is, Charset.forName("UTF-8"));
            br = new BufferedReader(isr);
            String data = null;
            while ((data = br.readLine()) != null) {
                System.out.println(data);
            }
        } catch (Exception e) {
            System.out.println("启动成功，如果您想自定义启动成功标识，您可以在工程src/resources下创建success.txt文件，并写入启动成功输出的信息");
        } finally {
            if (br != null) {
                br.close();
            }
            if (isr != null) {
                isr.close();
            }
            if (is != null) {
                is.close();
            }
        }
    }
}
